package com.example.zhongxiangmo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.security.Permission;
import java.security.Permissions;
import java.util.ArrayList;

public class HomeActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private LinearLayout mLl;
    private NavigationView mNv;
    private DrawerLayout mDl;
    private ViewPager mVpTab;
    private TabLayout mTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
        initListener();
    }

    //所有 监听
    private void initListener() {
        View headerView = mNv.getHeaderView(0);
        ImageView iv_head = headerView.findViewById(R.id.iv_head);
        iv_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(HomeActivity.this, "你好，宝贝", Toast.LENGTH_SHORT).show();
            }
        });
        mNv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.item1:
                        Toast.makeText(HomeActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item2:
                        Toast.makeText(HomeActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();

                        break;
                    case R.id.item3:
                        Toast.makeText(HomeActivity.this, "我的隐私已被禁止查看", Toast.LENGTH_SHORT).show();
                        mDl.closeDrawer(Gravity.LEFT);
                        break;
                    case R.id.item4:
                        Toast.makeText(HomeActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        break;
                }
                return false;
            }
        });
//        mTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                switch (tab.getPosition()){
//                    case 0:
//                        Toast.makeText(HomeActivity.this, tab.getText(), Toast.LENGTH_SHORT).show();
//                        break;
//                    case 1:
//                        Toast.makeText(HomeActivity.this, tab.getText(), Toast.LENGTH_SHORT).show();
//
//                        break;
//                }
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
    }

    //初始化界面
    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mLl = (LinearLayout) findViewById(R.id.ll);
        mNv = (NavigationView) findViewById(R.id.nv);
        mDl = (DrawerLayout) findViewById(R.id.dl);
        mVpTab = (ViewPager) findViewById(R.id.vp_tab);
        mTab = (TabLayout) findViewById(R.id.tab);

        //设置toolbar
        setToolbar();


        ArrayList<Fragment> fragments = new ArrayList<>();
        HomeFragment homeFragment = new HomeFragment();
        XiangMuFragment myFragment = new XiangMuFragment();
        DuoBuJuFragment duoBuJuFragment = new DuoBuJuFragment();
        ErJiFragment erJiFragment = new ErJiFragment();
        fragments.add(homeFragment);
        fragments.add(duoBuJuFragment);
        fragments.add(erJiFragment);
        fragments.add(myFragment);

        HomeAdapter homeAdapter = new HomeAdapter(getSupportFragmentManager(), fragments);

        mVpTab.setAdapter(homeAdapter);

        mTab.setupWithViewPager(mVpTab);


        mTab.getTabAt(0).setText("首页").setIcon(R.mipmap.ic_launcher);
        mTab.getTabAt(1).setText("多布局").setIcon(R.mipmap.ic_launcher_round);
        mTab.getTabAt(2).setText("二级列表").setIcon(R.mipmap.ic_launcher_round);
        mTab.getTabAt(3).setText("项目").setIcon(R.mipmap.ic_launcher_round);

    }

    //toolbar设置
    private void setToolbar() {
        mToolbar.setTitle("我命由我不由天");
        mToolbar.setTitleTextColor(getResources().getColor(R.color.colorWitht));

        setSupportActionBar(mToolbar);

        mNv.setItemIconTintList(null);

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDl, mToolbar, R.string.app_name, R.string.app_name);
        mDl.addDrawerListener(actionBarDrawerToggle);

        actionBarDrawerToggle.syncState();
    }

    //三个点
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "线性布局");
        menu.add(0, 1, 0, "网格布局");
        menu.add(0, 2, 0, "瀑布流布局");
        menu.add(0, 3, 0, "我的收藏");


        return super.onCreateOptionsMenu(menu);
    }

    //监听三个点
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;

            case 1:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();

                break;
            case 2:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();

                break;
            case 3:

                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
        }

        return super.onOptionsItemSelected(item);
    }
}
